/**
 * 金额格式处理相关函数
 */

/**
 * 
 * @param {}
 *            inStr
 * @return {String}
 */
function standMoney(inStr) {
	var i, charValue, outStr;
	for (i = 0; i < inStr.length; i++) {
		charValue = inStr.charAt(i);
		if (isNaN(parseInt(charValue, 10)) && (charValue != ".")
				&& (charValue != ",")) {
			// 判断是否是负数
			if (i == 0 && charValue == "-") {
				continue;
			}
			if (inStr != "NaN") {
				alert(inStr + " 非法金额!");
			}
			return "0.00";
		}
	}
	// 以小数点为分界，分别处理整数和小数部分
	var valueArr;
	valueArr = inStr.split(".");
	if (valueArr.length > 2) {
		alert(inStr + " 非法金额!");
		return "0.00";
	}

	// 处理小数部分
	var dotStr, dotValue;
	if (valueArr.length == 2) {
		dotValue = valueArr[1];
		if (dotValue.length == 0) {
			dotStr = "00";
		} else {
			if (dotValue.length == 1){
				dotStr = dotValue + "0";
			}else if(dotValue.length == 2){
			    dotStr = dotValue.substring(0, 2);
			}else{
			    dotStr = Math.round(parseInt(dotValue.substring(0, 3),10)/10);
			    if(dotStr < 10){
                   dotStr = '0'+dotStr;
                }
			}
		}
	} else {
		dotStr = "00";
	}
	// 处理整数部分
	var minus = "";// 数字的符号位
	var intArr;
	intArr = valueArr[0].split(",");
	// 无论整数部分是否已经用逗号分隔开，都将其合并成一个整体
	var intValue;
	intValue = "";
	for (i = 0; i < intArr.length; i++) {
		intValue += intArr[i];
	}
	// 处理符号位
	var m = intValue.indexOf("-");
	if (m != -1) {
		minus = "-"
		intValue = intValue.substring(1, intValue.length);
	}

	// 将整数部分用逗号进行分隔
	var intStr = "";
	while (intValue.length > 3) {
		intStr = "," + intValue.substring(intValue.length - 3, intValue.length)
				+ intStr;
		intValue = intValue.substring(0, intValue.length - 3);
	}
	intStr = intValue + intStr;

	// 最后将处理后的整数部分与小数部分合并，作为输出
	if (intStr == "" || intStr == null) {
		intStr = "0"
	}
	outStr = minus + intStr + "." + dotStr;
	return outStr;
}

/**
 * 恢复成数字串的格式，删掉分节符
 * 
 * @param {}
 *            inStr
 * @return {}
 */
function revertMoneyString(inStr) {
	var outStr = "";
	var ch;

	for (i = 0; i < inStr.length; i++) {
		ch = inStr.charAt(i);
		if (ch != ',') {
			outStr += ch;
		} else {
			continue;
		}
	}
	return outStr;
}

/**
 * 金额模式恢复成integer
 * 
 * @param {}
 *            inStr
 * @return {}
 */
function revertToString(inStr) {
	var outStr = revertMoneyString(inStr);
	var j = outStr.indexOf('.');
	if (j == -1) {
		return outStr;

	} else {
		outStr = outStr.substring(0, j);
		return outStr;
	}
}
/**
 * 添加比例%
 * 
 * @param {}
 *            inStr
 * @return {}
 */
function revertBelielString(inStr) {
	for (i = 0; i < inStr.length; i++) {
		charValue = inStr.charAt(i);
		if (i == 0) {
			if (charValue == '-') {
				continue;
			} else if (isNaN(parseInt(charValue, 10)) && (charValue != ".")
					&& (charValue != ",")) {
				if (i == inStr.length - 1 && charValue == "%") {
					return inStr;
				}
				alert(inStr + "不是数字!");
				return "0.00%";
			}
		} else if (isNaN(parseInt(charValue, 10)) && (charValue != ".")
				&& (charValue != ",")) {
			if (i == inStr.length - 1 && charValue == "%") {
				return inStr;
			}
			alert(inStr + "不是数字!");
			return "0.00%";
		}
	}
	return inStr + '%';
}
/**
 * 添加比例%(支持负数)
 * 
 * @param {}
 *            inStr
 * @return {}
 */
function revertNetBelielString(inStr) {
	for (i = 0; i < inStr.length; i++) {
		charValue = inStr.charAt(i);
		if (i == 0) {
			if (charValue == '-') {
			} else if (isNaN(parseInt(charValue, 10)) && (charValue != ".")
					&& (charValue != ",")) {
				if (i == inStr.length - 1 && charValue == "%") {
					return inStr;
				}
				alert(inStr + "不是数字!");
				return "0.00%";
			}
		} else if (isNaN(parseInt(charValue, 10)) && (charValue != ".")
				&& (charValue != ",")) {
			if (i == inStr.length - 1 && charValue == "%") {
				return inStr;
			}
			alert(inStr + "不是数字!");
			return "0.00%";
		}
	}

	return inStr + '%';
}
/**
 * 去除百分比负号%
 * 
 * @param {}
 *            inStr
 * @return {}
 */
function removeBelielString(inStr) {
	var i = inStr.toString().indexOf('%');
	if (i != -1) {
		var outValue = inStr.split("%");
		return outValue[0];
	}else{
		return inStr.toString();
	}
}

/**
 * 检查字符串是否是数字
 * 
 * @param {}
 *            inStr
 * @return {}
 */

function isNumber(str) {
	if (!isNaN(str) && str.indexOf('e') == -1 && str.indexOf('E') == -1) {
		return true;
	}
	return false;
}